package com.itheima.controller;

import cn.hutool.core.lang.Assert;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.pojo.Result;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.SmsUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Map;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisUtil jedisUtil;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result send4Order(@RequestBody Map<String,String> validate) {
        try {
            String imgCode = validate.get("imgCode");
            String deviceId = validate.get("deviceId");
            String imgCodeCheck = jedisUtil.get(deviceId);
            if (imgCodeCheck == null || !imgCodeCheck.equals(imgCode)) {
                return Result.error("图形验证码错误，请重新输入");
            }
            String telephone = validate.get("telephone");
            String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
//        SmsUtil.sendSmsCode(telephone, code);
            System.out.println("code = " + code);
            jedisUtil.addTimeLimited(telephone+ RedisMessageConstant.SENDTYPE_ORDER,30,code);
            return Result.success("验证码发送成功，请注意查收");
        } catch (Exception e) {
            return Result.error("验证码发送失败，请检查手机号输入是否正确");
        }
    }
    @RequestMapping("/send4Login")
    public Result send4Login(@RequestBody Map<String,String> validate) {
        try {
            String imgCode = validate.get("imgCode");
            String deviceId = validate.get("deviceId");
            String imgCodeCheck = jedisUtil.get(deviceId);
            if (imgCodeCheck == null || !imgCodeCheck.equals(imgCode)) {
                return Result.error("验证码错误，请重新输入");
            }
            String telephone = validate.get("telephone");
            String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
//        SmsUtil.sendSmsCode(telephone, code);
            System.out.println("code = " + code);
            jedisUtil.addTimeLimited(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,60*10,code);
            return Result.success("验证码发送成功，请注意查收");
        } catch (Exception e) {
            return Result.error("验证码发送失败，请检查手机号输入是否正确");
        }
    }


    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis
        jedisPool.getResource().setex(deviceId,60*10,text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }
}
